%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Reproducing Table 5 of the paper:
%Reis, Ricardo (2006) "Inattentive Producers," Review of Economic Studies.
%Written by: Ricardo Reis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This program produces the actual numbers in the Table
% Needs: alpha, lambda, upper, iterations, csi, eta, gamma, rho
% Uses InPro_data_pre.txt -- the pre-war data from Romer.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%STEP 1: LOAD DATA AND GET INNOVATIONS

%1st column has index, 2nd has dates, 3rd has m, 4th has p, 5th has a, 6th has g_m, 7th has pi, 8th has g_a
load InPro_data_pre.txt
n=length(InPro_data_pre);
g_a=InPro_data_pre(21:n,8);
a=InPro_data_pre(20:n,5);
g_m=InPro_data_pre(:,6);
pi=InPro_data_pre(:,7);
pi2=InPro_data_pre(21:n,7);
g_m2=InPro_data_pre(21:n,6);
n2=length(pi2);
%Constant from both components
pred_cons=mean(g_m)/4-mean(g_a)/4;
%Variances to draw from
var_g_m=var(g_m)/44; %44 because rw of average
var_a=var(a)/4;

%STEP 2: ITERATE OVER MANY POSSIBLE HISTORIES FOR MODEL

moments=[];
for i=1:iterations
    % STEP 2a: BUILD NEW SERIES FOR THE INNOVATIONS
    tech_shock=normrnd(0,sqrt(var_a),4*n2+upper,1);
    m_shock=normrnd(0,sqrt(var_g_m),4*n2+upper,1);
    % construct g_a and g_m series
    for j=1:4*n2
        pred_g_a(j,1)=mean(g_a)+tech_shock(upper+j,1)-tech_shock(upper+j-1,1);
        pred_g_m(j,1)=mean(g_m)+m_shock(upper+j,1);
    end
    % STEP 2b: RUN IT
    for j=1:4*n2
        pred_pi_m(j,1)=gamma'*flipud(m_shock(j:upper+j,1));
        pred_pi_tech(j,1)=csi'*flipud(tech_shock(j:upper+j,1));
        pred_pi(j,1)=pred_pi_m(j,1)+pred_pi_tech(j,1);
    end
    pred_pi=pred_pi+pred_cons*ones(4*n2,1);
    clear j pred_pi_m pred_pi_tech
    % Step 2c: TURN IT INTO ANNUAL
    for j=1:n2
        pred_pi_a(j,1)=sum(pred_pi(4*(j-1)+1:4*j,1));
        pred_g_m_a(j,1)=sum(pred_g_m(4*(j-1)+1:4*j,1));
        pred_g_a_a(j,1)=sum(pred_g_a(4*(j-1)+1:4*j,1));
    end
    pred_pi=pred_pi_a;
    pred_g_m=pred_g_m_a;
    pred_g_a=pred_g_a_a;
    clear pred_g_m_a pred_g_a_a pred_pi_a j
    % STEP 2d: ANALYSE FIT
    t=length(pred_pi);
    %moments
    mom1=std(pred_pi);
    mom2=corrcoef(pred_pi(2:t),pred_pi(1:t-1));
    mom2=mom2(1,2);
    mom3=corrcoef(pred_pi,pred_g_m);
    mom3=mom3(1,2);
    mom4=corrcoef(pred_pi(2:t),pred_g_m(1:t-1));
    mom4=mom4(1,2);
    mom5=corrcoef(pred_pi(1:t-1),pred_g_m(2:t));
    mom5=mom5(1,2);
    mom6=corrcoef(pred_pi,pred_g_a);
    mom6=mom6(1,2);
    mom7=corrcoef(pred_pi(2:t),pred_g_a(1:t-1));
    mom7=mom7(1,2);
    mom8=corrcoef(pred_pi(1:t-1),pred_g_a(2:t));
    mom8=mom8(1,2);
    % STEP 2e: STORING
    moments=[moments; mom1 mom2 mom3 mom4 mom5 mom6 mom7 mom8];
    clear mom1 mom2 mom3 mom4 mom5 mom6 mom7 mom8
end

%STEP 3: OBTAIN MOMENTS

legend=['     st dev','     acorr','    corr_m','  corr_m_lead',' corr_m_lag',' corr_a',' corr_a_lead',' corr_a_lag'];
%Model
Model=mean(moments);
temp=sort(moments);
Model05=temp(floor(0.05*iterations),:);
Model95=temp(ceil(0.95*iterations),:);
%Data
mom1=std(pi2);
mom2=corrcoef(pi2(2:n2),pi2(1:n2-1));
mom2=mom2(1,2);
mom3=corrcoef(pi2,g_m2);
mom3=mom3(1,2);
mom4=corrcoef(pi2(2:n2),g_m2(1:n2-1));
mom4=mom4(1,2);
mom5=corrcoef(pi2(1:n2-1),g_m2(2:n2));
mom5=mom5(1,2);
mom6=corrcoef(pi2,g_a);
mom6=mom6(1,2);
mom7=corrcoef(pi2(2:n2),g_a(1:n2-1));
mom7=mom7(1,2);
mom8=corrcoef(pi2(1:n2-1),g_a(2:n2));
mom8=mom8(1,2);
Data=[mom1 mom2 mom3 mom4 mom5 mom6 mom7 mom8];
clear mom1 mom2 mom3 mom4 mom5 mom6 mom7 mom8 temp
%Measurement error adjustments
error=sqrt(Data(1,1)^2-Model(1,1)^2);
adjustment=error/Model(1,1);
a1=sqrt(Model(1,1)^2+error^2);
a2=Model(1,2)/(1+adjustment^2);
a3=Model(1,3)/(1+adjustment);
a4=Model(1,4)/(1+adjustment);
a5=Model(1,5)/(1+adjustment);
a6=Model(1,6)/(1+adjustment);
a7=Model(1,7)/(1+adjustment);
a8=Model(1,8)/(1+adjustment);
Measur=[a1 a2 a3 a4 a5 a6 a7 a8];
clear a1 a2 a3 a4 a5 a6 a7 a8 adjustment error